import { getAllArticleList, getArchives, getArticleList, getArticleRecommend, getArticleViewList ,getArticleCategoryFront, getfiling} from '@/services';
import { allArticleItem, ArchivesItem, articleRecommentItem, filingItem, IArticleCategoryFrontItem, IArticleDetailItem, IArticleItem } from '@/types';
import { Effect, Reducer } from 'umi';

export interface ArticleModelState {
  articleRecomment: articleRecommentItem[]
  articleList: IArticleItem[]
  articleLength: number,
  allArticleList: allArticleItem[]
  archivesList:ArchivesItem[],
  articleDetail:IArticleDetailItem,
  categoryFront:IArticleCategoryFrontItem[],
  categoryFrontLenght:number,
  allfiling:filingItem[]
}

export interface ArticleModelType {
  namespace: 'article';
  state: ArticleModelState;
  effects: {
    getArticleList: Effect;
    getArticleRecommend: Effect;
    getAllArticleList: Effect;
    getArchivesList:Effect;
    getArticleViewList:Effect;
    getArticleCategoryFrontList:Effect;
    getFiling:Effect;
  };
  reducers: {
    save: Reducer<ArticleModelState>;

  };

}

const ArticleModel: ArticleModelType = {
  namespace: 'article',

  state: {
    allArticleList: [],
    articleList: [],
    articleLength: 0,
    articleRecomment: [],
    archivesList:[],
    articleDetail:{} as IArticleDetailItem,
    categoryFront:[],
    categoryFrontLenght:0,
    allfiling:[]
  },
  //副作用
  effects: {
    *getArticleRecommend({ payload }, { call, put }) {
      const result = yield call(getArticleRecommend)
      if (result.data) {
        yield put({
          type: 'save',
          payload: {
            articleRecomment: result.data,
          }
        })
      }
    },
    *getFiling({ payload }, { call, put }) {
      const result = yield call(getfiling)
      // debugger
      if (result.data) {
        yield put({
          type: 'save',
          payload: {
            allfiling: result.data[2022].January
          }
        })
      }
    },
    *getArticleList({ payload }, { call, put }) {
      const result = yield call(getAllArticleList)
      if (result.data) {
        yield put({
          type: 'save',
          payload: {
            articleList: result.data,
            articleLength: result.data[1]
          }
        })
      }
    },
    *getAllArticleList({ }, { call, put }) {
      const result = yield call(getAllArticleList)
      

      if (result.data) {
        yield put({
          type: 'save',
          payload: {
            allArticleList: result.data,
          }
        })
      } 
    },
    *getArchivesList({payload},{call,put}){
      const result = yield call(getArchives)
  
      if (result.data) {
        yield put({
          type: 'save',
          payload: {
            archivesList: result.data,
          }
        })
      }
    },
    *getArticleViewList({payload},{call,put}){
      const result = yield call(getArticleViewList,payload)
     
      if (result.data) {
        yield put({
          type: 'save',
          payload: {
            articleDetail: result.data,
          }
        })
      }
    },
    *getArticleCategoryFrontList({payload},{call,put}){
       const result = yield call(getArticleCategoryFront,payload)
       
     
      if (result.data) {
        yield put({
          type: 'save',
          payload: {
            categoryFront: result.data[0],
            categoryFrontLenght: result.data[1]
          }
        })
      }
    }
  },
  //同步修改state的操作
  reducers: {
    save(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },

  },

};

export default ArticleModel;